package com.ymatou.iapi.sellerproduct.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.JsonObject;
import com.ymt.core.tool.Logger;
import com.ymttest.business.YmatouAutoTestCall;
import com.ymttest.business.service.AssertService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.ProductListingModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
/**
*更改规格库存Call
*FreeMaker Template自动生成代码
*/
public class UpdateProductCatalogsCall extends YmatouAutoTestCall{
	private static String opurl ="/api/SellerProduct/UpdateProductCatalogs";
	
	public UpdateProductCatalogsCall() {
		super(EnvSetup.getSystemUrl("sellerproduct_iapi"),opurl,"POST","JSON");
	}
	@Override
	public void addDefHeader() {
		client.addHeader("Accept","text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}
		

	public static void main(String[] args) {
		new UpdateProductCatalogsCall().callAndGetReturnData();
	}
	
	
	
	
	
	
	
	
	/**
	 * UpdateProductCatalogs SQL & Mongo 校验
	 * @param result 传入请求报文
	 * @throws Exception
	 */
	public static void checkUpdateProductCatalogs(JSONObject result) throws Exception{
		
		String productID = result.getString("ProductID");
		
		//Ymt_Products
		YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
		List<YmtProductsI> ymtProductsI = ymtProductsIWapper.selectProductByProductId(productID);
				
		//Ymt_Catalogs表
		YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
		BigDecimal maxfQuotePrice = ((BigDecimal) ymtCatalogsIWapper.selectMaxfQuotePrice(productID).get(0).get("MaxPrice")).setScale(2, BigDecimal.ROUND_HALF_UP);//最大单价
		BigDecimal minfQuotePrice = ((BigDecimal) ymtCatalogsIWapper.selectMinfQuotePrice(productID).get(0).get("MinPrice")).setScale(2, BigDecimal.ROUND_HALF_UP);//最小单价
		
		////////////////检查SQL 
		//链接SQL
		for (int i = 0; i < result.getJSONArray("Catalogs").length(); i++) {
			
			String catalogsID = result.getJSONArray("Catalogs").getJSONObject(i).getString("CatalogId");
			//Ymt_Catalogs
			List<YmtCatalogsI> catalogslist1 = ymtCatalogsIWapper.selectCatalogsByCatalogIdforList(catalogsID);			
			YmtCatalogsI ymtCatalogs1=catalogslist1.get(0);
			Map<String, Object> actRetMap  = MapUtil.pojoToMap(ymtCatalogs1);
			
			//断言校验			
			Map<String, String>  sqlMap = new HashMap<String , String>();		
			sqlMap.put("Price", "fQuotePrice");
			sqlMap.put("VipPrice", "fVipPrice");
			sqlMap.put("NewCustomerPrice", "fNewCustomerPrice");
			if (ymtProductsI.get(0).getiCatalogType() != 2) {
				//FBX商品无法修改库存，iCatalogType=2为FBX商品
				sqlMap.put("Num", "iNum");
			}		
			
				JSONObject expRetMap = result.getJSONArray("Catalogs").getJSONObject(i);				
				AssertService.assertResultEqual(sqlMap,expRetMap, actRetMap);				
		
				
				
				
			if (YmtMongoDriverFactroy.isNewMongoCheck()){	
				
				
				//Mongo 新库
				ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
				Logger.verifyEquals(1, productDetailModelWapper.getCountByProductId(productID), "商品数据应该只有1条");
				
				//mongo表productDetailModel，根据商品id查询
				Map<String, Object> productDetailModelMap = productDetailModelWapper.getProductId(productID);

				//Mongo CatalogList jsonarry
				JSONArray mongoCatalogList = new JSONArray(productDetailModelMap.get("CatalogList").toString());  
				
				//根据规格id，取出mongo中相对应的mongo cataloglist
				JSONObject mongoCatalogforonly = returnOneCatalogbyMongo(mongoCatalogList, catalogsID);

				//取出mongo中价格&库存
				JSONArray priceList = mongoCatalogforonly.getJSONArray("PriceList");
				JSONArray stockList = mongoCatalogforonly.getJSONArray("StockList");
				
				//断言校验															
				for(int k = 0;k < priceList.length();k++){
					
					if (priceList.getJSONObject(k).getInt("PriceType") == 0 ) {
						double pricebymongo=Double.parseDouble(priceList.getJSONObject(k).getString("Price").toString());
						
						Logger.verifyEquals("原价", priceList.getJSONObject(k).getString("Reason"), "Mongo商品价格类别对比"+k);
						Logger.verifyEquals(expRetMap.getDouble("Price"), pricebymongo, "newMongo商品原价对比"+k);
					}
															
					else if (priceList.getJSONObject(k).getInt("PriceType") == 1) {
						double newCustomerPricebymongo=Double.parseDouble(priceList.getJSONObject(k).getString("Price").toString());
						
						Logger.verifyEquals("新客价", priceList.getJSONObject(k).getString("Reason"), "Mongo商品价格类别对比"+k);
						Logger.verifyEquals(expRetMap.getDouble("NewCustomerPrice"), newCustomerPricebymongo, "newMongo商品新客价对比"+k);
					}
															
					else if (priceList.getJSONObject(k).getInt("PriceType") == 2) {
						double vipPricebymongo=Double.parseDouble(priceList.getJSONObject(k).getString("Price").toString());								
						
						Logger.verifyEquals("VIP价", priceList.getJSONObject(k).getString("Reason"), "Mongo商品价格类别对比"+k);
						Logger.verifyEquals(expRetMap.getDouble("VipPrice"), vipPricebymongo, "newMongo商品VIP价对比"+k);
					}																	
				}
								
				if (ymtProductsI.get(0).getiCatalogType() == 2) {
					//FBX商品无法修改库存，iCatalogType=2为FBX商品
					int stockNum = stockList.getJSONObject(0).getInt("Stock");
					Logger.verifyEquals(catalogslist1.get(0).getiNum() ,stockNum, "newMongo验证商品库存");
				}else{
					int stockNum = stockList.getJSONObject(0).getInt("Stock");
					Logger.verifyEquals(expRetMap.getInt("Num"),stockNum, "newMongo验证商品库存");	
				}
				
				Map<String, Object> tgtRetMap = new HashMap<String, Object>();
				tgtRetMap.put("MaxCatalogPrice", String.valueOf(maxfQuotePrice));	//最大单价
				tgtRetMap.put("MinCatalogPrice", String.valueOf(minfQuotePrice));	//最小单价
				AssertService.assertResultEqual(tgtRetMap, productDetailModelMap);
				
				//商品简表
				Map<String, Object> productListingModel = new ProductListingModelWapper().getProductId(productID);
				Map<String, Object> tgtRetMap1 = new HashMap<String, Object>();
				tgtRetMap1.put("MaxCatalogPrice", String.valueOf(maxfQuotePrice));	//最大单价
				tgtRetMap1.put("MinCatalogPrice", String.valueOf(minfQuotePrice));	//最小单价
				AssertService.assertResultEqual(tgtRetMap, productListingModel);				
			}		
		}
	}
	
	
	
	
	
	
	
	

	
	/**
	 * 根据规格id，取出mongo中相对应的mongo cataloglist
	 * @param mongoCatalogList 传入mongoCatalogList JSONArry型
	 * @param catalogid 规格id
	 * @return 返回mongo中规格id相对应的mongo cataloglist
	 * @throws Exception
	 */
	public static JSONObject returnOneCatalogbyMongo(JSONArray mongoCatalogList,String catalogid) throws Exception {
	    try {
		    	        

		    	JSONObject jCatalogList = null;
		    	for (int i = 0; i < mongoCatalogList.length(); i++) {
		    		if (catalogid.equals(mongoCatalogList.getJSONObject(i).getString("CatalogId"))) {
		    			jCatalogList = mongoCatalogList.getJSONObject(i);
		    		}
		    	}			
		    	return jCatalogList;
						
	    }catch (Exception e) {
	        e.printStackTrace();
	    }
		return null;
	}	
	
	
	
	
	
	
	
	
	
	
	
}